package com.example.rentalclearing.service.impl;

import com.example.rentalclearing.entity.BankCollectionRecord;
import com.example.rentalclearing.mapper.BankCollectionRecordMapper;
import com.example.rentalclearing.service.IBankCollectionRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 银行收款表 服务实现类
 * </p>
 *
 * @author sjf
 * @since 2025-10-17
 */
@Slf4j
@Service
public class BankCollectionRecordServiceImpl extends ServiceImpl<BankCollectionRecordMapper, BankCollectionRecord> implements IBankCollectionRecordService {

    /**
     * 使用悲观锁查询未用完的银行收款记录，按ID排序以避免死锁
     */
    @Override
 public List<BankCollectionRecord> selectUnusedByPayerAccountName(String tenantName) {
        return lambdaQuery()
                .eq(BankCollectionRecord::getPayerAccountName, tenantName)
                .ne(BankCollectionRecord::getUsageStatus, "fully_used")
                .orderByAsc(BankCollectionRecord::getId) // 按ID排序避免死锁
                .list();
    }

}